package com.cn.io.socket;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.charset.StandardCharsets;

public class DatagramServer {

    public static void main(String[] args) throws IOException {
        DatagramChannel server = DatagramChannel.open();
        server.socket().bind(new InetSocketAddress(25000));

        ByteBuffer receiveBuffer = ByteBuffer.allocate(1024);
        while (true) {
            receiveBuffer.clear();
            SocketAddress receiveAddress = server.receive(receiveBuffer);//当没有数据接受时，此处会阻塞
            receiveBuffer.flip();
            System.out.println(receiveAddress.toString());
            System.out.println(StandardCharsets.UTF_8.decode(receiveBuffer));
        }
    }
}
